/** 各入口文件应该引入该文件替代手动引入 */
import '@src/common/polyfill';
import '@src/modules/linkc/assets/public.scss';
// import 'publink-ui/lib/publink-ui.css';

import Vue from 'vue';
// import VueCompositionAPI from 'vue'
import i18n from '@src/locales';

import directive from '@src/directive';
import component from '@src/component';
import filter from '@src/filter';
import BaseGallery from 'packages/BaseGallery';

import appConfig from 'app.config';
import platform from '@src/platform';
import platformExtension from '@src/platform/extension'
import { getRootWindow, removeClassByRegExp } from '@src/util/dom'
import { isQYWX, isOpenData, isDevelopment } from '@src/util/platform';
import globalMixin from '@src/mixins/globalMixin'
import { Fragment } from 'vue-frag'

Vue.component('fragment', Fragment)
Vue.component('Fragment', Fragment)
Vue.use(directive);
Vue.use(filter);
Vue.use(component);
// Vue.use(VueCompositionAPI);
Vue.mixin(globalMixin)

import 'quill/dist/quill.core.css';
import 'quill/dist/quill.snow.css';
import 'quill/dist/quill.bubble.css';
import '@src/assets/scss/index.scss';

import previeVideoCompoment from '@src/component/compomentV2/PreviewVideo/index.vue';
import previePDFCompoment from '@src/component/compomentV2/PreviePDF/index.vue';
import PrevieElementImgCompoment from '@src/component/compomentV2/PrevieElementImg/index.vue';
import WWOpenData from '@src/component/common/WWOpenData/WWOpenData.vue';

/** utils */
import { getThemeClass } from '@src/util/theme.ts';
import domGuide from '@src/common/Guide'

/* 异常监控 */
import { errorHandler, production } from '@src/util/onerror/index.js'
// if(production()){
//   Vue.config.errorHandler = errorHandler
// }

/* 微信通讯录组件本地开发环境兼容 */
if (isDevelopment() && isOpenData) {
  Vue.component(WWOpenData.name, WWOpenData)
}

function previewImg(url) {
  if (!url) return;
  if (url.target && url.target.nodeName == 'IMG')
    return BaseGallery.preview(url.target);
  let imgDom = document.createElement('img');
  imgDom.src = url;
  BaseGallery.preview(imgDom);
}

/**
 * 使用Element image-view预览图片
 * @param {Array} url 图片地址
*/
export function previewElementImg(url = '', urls = []) {
  let PrevieElementImg = Vue.extend(PrevieElementImgCompoment);

  let divDom = document.createElement('div');
  divDom.id = 'normal-preview-ElementImg';
  divDom.ref = 'previewElementImg';
  if (!document.getElementById('normal-preview-ElementImg')) {
    // this.$el.appendChild(divDom);
    document.body.appendChild(divDom)
  }

  new PrevieElementImg({
    i18n,
    data() {
      return {};
    },
    propsData: {
      url,
      urls
    },
    methods: {},
  }).$mount('#normal-preview-ElementImg');
}

/**
 * 
 * @param {String} url 视频地址
 * @param {Object} info 视频详细信息
*/
function previewVideo(url, info = {}) {
  let PrevieVideoCompoments = Vue.extend(previeVideoCompoment);

  let divDom = document.createElement('div');
  divDom.id = 'normal-preview-video';
  divDom.ref = 'previeVideoCompoment';
  if (!document.getElementById('normal-preview-video')) {
    document.body.appendChild(divDom);
  }

  // }
  new PrevieVideoCompoments({
    i18n,
    data() {
      return {};
    },
    propsData: {
      url,
      info
    },
    methods: {},
  }).$mount('#normal-preview-video');
}

/**
 * 
 * @param {String} url PDF地址
 * @param {Object} info PDF详细信息
*/
function previewPDF(url, info = {}) {
  let previePDFCompoments = Vue.extend(previePDFCompoment);

  let divDom = document.createElement('div');
  divDom.id = 'normal-preview-pdf';
  divDom.ref = 'previePDFCompoment';
  if (!document.getElementById('normal-preview-pdf')) {
    document.body.appendChild(divDom);
  }

  // eslint-disable-next-line new-cap
  new previePDFCompoments({
    i18n,
    data() {
      return {};
    },
    propsData: {
      url,
      info
    },
    methods: {},
  }).$mount('#normal-preview-pdf');
}

function getUrlObj(window_) {
  let params = window_.location.href.split('?')[1];
  let obj = {};
  try {
    if (params) {
      params.split('&').forEach((item) => {
        let item_ = item.split('=');
        obj[item_[0]] = item_[1];
      });
    }
  } catch (error) {
    console.warn(error);
  }
  return obj;
}

Vue.prototype.$appConfig = appConfig;
Vue.prototype.$platform = platform;
Vue.prototype.$previewImg = previewImg;
Vue.prototype.$previewElementImg = previewElementImg;
Vue.prototype.$previewVideo = previewVideo;
Vue.prototype.$previewPDF = previewPDF;
Vue.prototype.$getUrlObj = getUrlObj;
Vue.prototype.$Guide = domGuide;

// 主题设置 todo: 还是有明显的切换
(async function () {
  let themeClass = await getThemeClass();
  let body = document.getElementsByTagName('body')[0];
  if (body) {
    removeClassByRegExp(body, /theme-*/);
    body.classList.add(themeClass);
  }
})();

let body = document.getElementsByTagName('body')[0];
if (body) {
  body.className = `${body.className} theme-default`
}

// 微信配置
if (window.wx || isQYWX()) {
  const rootWindow = getRootWindow(window)
  if (rootWindow.wxConfig) {
    // platformExtension.injectAuth(rootWindow.wxConfig)
  } else {
    platformExtension.sign()
  }
}

export default Vue;
